package vn.edu.uit.hotelbooking.adapter;

import java.util.ArrayList;

import vn.edu.uit.hotelbooking.R;
import vn.edu.uit.hotelbooking.model.Hotel;
import vn.edu.uit.hotelbooking.utils.Image.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HotelAdapter extends ArrayAdapter<Hotel>{
    private Context context;
    private Hotel hotel = null;
    private ArrayList<Hotel> arrayListHotel = null;
    private int layoutResourceId;   
    public ImageLoader imageLoader; 
    
    public HotelAdapter(Context context,int layoutResourceId, ArrayList<Hotel> hotel) {
        super(context, R.layout.hotel_item, hotel);
        this.context = context;
        this.layoutResourceId = layoutResourceId;
        this.arrayListHotel = hotel;
        imageLoader=new ImageLoader(context);
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        hotel = arrayListHotel.get(position);
        HotelHolder hotelHolder = null;
       
        if(row == null)
        {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(layoutResourceId, parent, false);
           
            hotelHolder = new HotelHolder();
            //hotelHolder.pbHotelThumbnail = (ProgressBar)row.findViewById(R.id.pbHotelThumbnail_HoteItem);
            hotelHolder.rlHotelItem = (RelativeLayout)row.findViewById(R.id.rlHotelItem);
            hotelHolder.tvHotelName = (TextView)row.findViewById(R.id.tvHotelName_HotelItem);
            hotelHolder.ivFavorite = (ImageView)row.findViewById(R.id.ivFavourite_HotelItem);
            hotelHolder.ivHotelThumb = (ImageView)row.findViewById(R.id.ivHotelThumb_HotelItem);
            hotelHolder.ivHotelStar = (ImageView)row.findViewById(R.id.ivHotelStar_HotelItem);
            hotelHolder.tvHotelStarValue = (TextView)row.findViewById(R.id.tvHotelStarValue_HotelItem);
            hotelHolder.ivRate = (ImageView)row.findViewById(R.id.ivRate_HotelItem);
            hotelHolder.tvRateValue = (TextView)row.findViewById(R.id.tvRateValue_HotelItem);
            hotelHolder.ivAddress = (ImageView)row.findViewById(R.id.ivAddress_HotelItem);
            hotelHolder.tvAddressValue = (TextView)row.findViewById(R.id.tvAddressValue_HotelItem);
            hotelHolder.tvPrice = (TextView)row.findViewById(R.id.tvPrice_HotelItem);
            hotelHolder.ivDistance = (ImageView)row.findViewById(R.id.ivDistance_HotelItem);
            hotelHolder.tvDistanceValue = (TextView)row.findViewById(R.id.tvDistanceValue_HotelItem);
            
            //dat tag la hotel id
            hotelHolder.ivFavorite.setTag(hotel.getHotel_id());
            hotelHolder.rlHotelItem.setTag(position);
            row.setTag(hotelHolder);
            hotelHolder.ivFavorite.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    
                }
            });
        }
        else
        {
            hotelHolder = (HotelHolder)row.getTag();
        }     
        
        /*if(hotel.getFavorite()){
            hotelHolder.ivFavorite.setBackgroundResource(R.drawable.star_gold_128);
        }*/
        
        hotelHolder.tvAddressValue.setText(hotel.getHotel_address());
        hotelHolder.tvHotelName.setText(hotel.getHotel_name());
        hotelHolder.tvHotelStarValue.setText(String.valueOf(hotel.getHotel_rating()));
        hotelHolder.tvPrice.setText(String.valueOf(hotel.getHotel_min_price()));
        hotelHolder.tvDistanceValue.setText(String.valueOf(hotel.getDistance()));
        //set tag imageview hotel thumbnail is image url
        hotelHolder.ivHotelThumb.setTag(hotel.getHotel_thumbnail());
        imageLoader.displayImage(hotel.getHotel_thumbnail(), hotelHolder.ivHotelThumb);
        
        /*
        if(yes.equals(bainghe.getBookmark())){
            holder.imgBookmark.setBackgroundResource(R.drawable.bookmarked);
            Log.d("--ten bai nghe so sanh: ", bainghe.getFilePath());
        }
        else    
            holder.imgBookmark.setBackgroundResource(R.drawable.none_bookmark);
        if(yes.equals(bainghe.getDaLam())){
            holder.imgDalam.setVisibility(View.VISIBLE);
            holder.tvDiem.setText(bainghe.getDiem() + " điểm");
            holder.tvNgaylam.setText("Vào lúc " + bainghe.getNgayLam());
            holder.tvDiem.setVisibility(View.VISIBLE);
            holder.tvNgaylam.setVisibility(View.VISIBLE);
        }
        else
        {
            holder.imgDalam.setVisibility(View.INVISIBLE);
            holder.tvDiem.setVisibility(View.INVISIBLE);
            holder.tvNgaylam.setVisibility(View.INVISIBLE);
        }
        if(baingheSaved.contains(bainghe.getFilePath())){
            Log.d("ten bai nghe so sanh: ", bainghe.getFilePath());
            holder.imgSave.setVisibility(View.VISIBLE);
        }
        else   
            holder.imgSave.setVisibility(View.INVISIBLE);
        
        holder.stt.setText(bainghe.getStt());
        holder.tenbainghe.setText(bainghe.getTenBainghe());
        holder.llBox.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //lay position cua item 
                int which = -1;
                Object obj =  v.getTag();                   
                if(obj instanceof Integer){
                    which = ((Integer)obj).intValue();
                }
                if (which > -1) {
                    clickedIndex = which;
                }
                
                View view = lv.getChildAt(clickedIndex - lv.getFirstVisiblePosition());
                ImageButton imgSave = (ImageButton)view.findViewById(R.id.imgSave);
                
                //lay id bai nghe
                String id = "";
                Object id_obj = imgSave.getTag();
                if(id_obj instanceof String){
                    id = id_obj.toString();
                }
                if (!id.equals("")) {
                    bookmark = id;
                }                                       
                bookmarkHandler();
            }
        });
            */
        return row;
    }   
    public static class HotelHolder{
        RelativeLayout rlHotelItem;
        TextView tvHotelName;
        ImageView ivFavorite;
        ImageView ivHotelThumb;
        ImageView ivHotelStar;
        TextView tvHotelStarValue;
        ImageView ivRate;
        TextView tvRateValue;
        ImageView ivDistance;
        TextView tvDistanceValue;
        ImageView ivAddress;
        TextView tvAddressValue;
        TextView tvPrice;
    }
}
